package com.htl.controller;

import com.htl.entity.ProductInfo;
import com.htl.service.ProductCategoryService;
import com.htl.service.ProductInfoService;
import com.htl.util.ResultVOUtil;
import com.htl.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 韩天乐
 * @since 2021-11-13
 */

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductInfoService productInfoService;

    @GetMapping("/list")
    public ResultVO list(){
        return ResultVOUtil.success(this.productCategoryService.findAllProductCategoryVO());
    }

    @GetMapping("/findPriceById/{id}")
    public BigDecimal findPriceById(@PathVariable("id") Integer id){
        return this.productInfoService.findPriceById(id);
    }

    @GetMapping("/findById/{id}")
    public ProductInfo findById(@PathVariable("id") Integer id){
        return this.productInfoService.getById(id);
    }

    /**
     * 通过 id去减库存
     * @param id
     * @param quantity
     * @return
     */
    @PutMapping("/subStockById/{id}/{quantity}")
    public Boolean subStockById(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity
    ){
        return this.productInfoService.subStockById(id, quantity);
    }

}

